package com.sunshine.ssm.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * http请求工具类
 *
 * @FileName: HttpClientUtils.java
 * @author:WeiHui.Zhang
 * @Date: 2016年5月27日 上午9:12:02
 * @Version:V1.00
 */
public final class HttpClientUtils {

	private static final String USER_AGENT = "Mozilla/5.0";

	private static final CloseableHttpClient CLIENT = HttpClients.createDefault();

	private HttpClientUtils() {
	}

	/**
	 * @param parameterMap
	 * @return List<NameValuePair>
	 * @Name: getParam
	 * @Description:
	 * @author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年5月27日上午9:12:14
	 */
	private static List<NameValuePair> getParam(Map<String, String> parameterMap) {
		List<NameValuePair> nameValuePairs = new ArrayList<>();
		if (parameterMap != null) {
			Set<String> keySet = parameterMap.keySet();
			for (String key : keySet) {
				nameValuePairs.add(new BasicNameValuePair(key, parameterMap.get(key)));
			}
		}
		return nameValuePairs;
	}

	/**
	 * @param url
	 * @return String
	 * @Name: doGet
	 * @Description:
	 * @author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年5月27日上午9:12:20
	 */
	public static String doGet(String url) throws Exception {
		String result = null;
		HttpGet httpGet = new HttpGet(url);
		config(httpGet);
		HttpResponse response = CLIENT.execute(httpGet);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity);
		}
		return result;
	}

	/**
	 * @param url
	 * @param params
	 * @return String
	 * @Name: doPost
	 * @Description:
	 * @author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年5月27日上午9:12:27
	 */
	public static String doPost(String url, Map<String, String> params) throws Exception {
		String result = null;
		HttpPost httpPost = new HttpPost(url);
		UrlEncodedFormEntity postEntity;
		postEntity = new UrlEncodedFormEntity(getParam(params), "UTF-8");
		httpPost.setEntity(postEntity);
		config(httpPost);
		HttpResponse response = CLIENT.execute(httpPost);
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			HttpEntity entity = response.getEntity();
			result = EntityUtils.toString(entity, "UTF-8");
		}
		return result;
	}

	/**
	 * @param httpRequestBase void
	 * @Name: config
	 * @Description:
	 * @author: WeiHui.Zhang
	 * @Version: V1.00
	 * @Date: 2016年5月27日上午9:12:34
	 */
	private static void config(HttpRequestBase httpRequestBase) {
		httpRequestBase.setHeader("User-Agent", USER_AGENT);
		// 配置请求的超时设置
		RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(30000).setConnectTimeout(30000)
				.setSocketTimeout(30000).build();
		httpRequestBase.setConfig(requestConfig);
	}

}